package com.app.babygo.dbservice;

import com.app.babygo.exceptions.InternalDBException;
import com.app.babygo.exceptions.ObjectNotFoundException;
import com.app.babygo.obj.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class UserAdapter extends DBAdapter {
	
	// table info of the account
	protected static final String ACCOUNT_USERID = "userid";
	protected static final String ACCOUNT_NAME = "username";
	protected static final String ACCOUNT_PASSWORD = "password";
	protected static final String ACCOUNT_ACCESSLEVEL = "accesslevel";
	protected static String TAG = "UserAdapter";
	
	// table creation strings
	protected static final String DATABASE_CREATE_TABLE_ACCOUNT = "create table "
			+ DATABASE_TABLE_ACCOUNT + " ("
			+ ACCOUNT_USERID + " text primary key not null, "
			+ ACCOUNT_NAME + " text not null, "
			+ ACCOUNT_PASSWORD + " text not null, " 
			+ ACCOUNT_ACCESSLEVEL + " text not null);";

	public UserAdapter(Context context) {
		super(context);
	}

	/**
	 * User account section
	 * @throws InternalDBException 
	 */
	public static boolean createTable(SQLiteDatabase db){
		try {
			db.execSQL(DATABASE_CREATE_TABLE_ACCOUNT);
		} catch (SQLException e) {
			Log.e(TAG, e.getMessage());
			return false;
		}
		Log.d(TAG, "Child Table created.");
		return true;
	}
	
	public void initialAccount(User user) throws InternalDBException  {
		try{
			ContentValues initialValues = new ContentValues();
			initialValues.put(ACCOUNT_USERID, user.getUserid());
			initialValues.put(ACCOUNT_NAME, user.getUsername());
			initialValues.put(ACCOUNT_PASSWORD, user.getPassword());
			initialValues.put(ACCOUNT_ACCESSLEVEL, user.getAccesslevel());
			db.insert(DATABASE_TABLE_ACCOUNT, null, initialValues);
		} catch (SQLiteException e){
			throw new InternalDBException("Could not complete account initialization.");
		}
	}

	//Get user info
	public User getAthenticationInfo(String userid) throws InternalDBException, ObjectNotFoundException {
		
		User searchUser = new User();
		try{	
			String[] columns = { 
					ACCOUNT_USERID, 
					ACCOUNT_NAME, 
					ACCOUNT_PASSWORD,
					ACCOUNT_ACCESSLEVEL };
			String[] SearchArg = { userid };
			
			Cursor cursor = db.query(DATABASE_TABLE_ACCOUNT, columns, ACCOUNT_USERID + "=?", SearchArg, null, null, null);
	
			if (cursor != null){
				cursor.moveToFirst();
			}
				
			if (cursor.getCount() > 0)
			{
				searchUser.setUserid(userid);
				searchUser.setUsername(getString(cursor, ACCOUNT_NAME));
				searchUser.setPassword(getString(cursor, ACCOUNT_PASSWORD));
				searchUser.setAccesslevel(getString(cursor, ACCOUNT_ACCESSLEVEL));
			}
			else
			{
				//This shouldn't happen. we must log it
				Log.d(TAG, "User not found. User id: " + userid);
				throw new ObjectNotFoundException("This user is not exist.");
			}
			
		} catch (SQLiteException e){
			Log.e(TAG, e.getMessage());
			Log.d(TAG, "[User id: " + userid + "] detail: "+ e.toString());
			throw new InternalDBException("Could not get user information.");
			
		} catch (NullPointerException e){
			Log.e(TAG, e.getMessage());
			Log.d(TAG, "User id: " + userid);
			throw new ObjectNotFoundException("This user is not exist.");
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			throw new InternalDBException("Unhandled exception.");
		}
		
		if(searchUser.getUserid() == null){
			Log.d(TAG, "User not found. User id: " + userid);
			throw new ObjectNotFoundException("This user is not exist.");
		} 
		
		return searchUser;
	}

}
